@extends('layout.master')

@section('title','添加游戏')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')
    <div class="white-box">
        <form class="form-horizontal form-material" method="post" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <div class="form-group">
                        <label class="col-md-12">游戏名</label>
                        <div class="col-md-12">
                            <input type="text" name="name" id="name"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">总能量</label>
                        <div class="col-md-12">
                            <input type="text" name="sum_energy" id="sum-energy"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">本人每天培育次数</label>
                        <div class="col-md-12">
                            <input type="text" name="own_frequency" id="own-frequency"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">本人每次培育可增长能量</label>
                        <div class="col-md-12">
                            <input type="text" name="own_add_energy" id="own-add-energy"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">未注册好友每天培育次数</label>
                        <div class="col-md-12">
                            <input type="text" name="old_friend_frequency" id="old-friend-frequency"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">未注册好友每次培育可增长能量</label>
                        <div class="col-md-12">
                            <input type="text" name="old_friend_add_energy" id="old-friend-add-energy"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">注册好友每天培育次数</label>
                        <div class="col-md-12">
                            <input type="text" name="new_friend_frequency" id="new-friend-frequency"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-2 col-sm-2">
                    <div class="form-group">
                        <label class="col-md-12">注册好友每次培育可增长能量</label>
                        <div class="col-md-12">
                            <input type="text" name="new_friend_add_energy" id="new-friend-add-energy"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">操作名</label>
                        <div class="col-md-12">
                            <input type="text" name="operating_name" id="operating-name"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">游戏列表封面图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="cover-img-url" class="dropify" name="cover_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">背景图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="bg-img-url" class="dropify" name="bg_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">操作功能图片</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="operating-img-url" class="dropify" name="operating_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">第一层</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="floor-img-url" class="dropify" name="floor_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">第二层</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="front-img-url" class="dropify" name="front_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">浇水时的图片</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="click-img-url" class="dropify" name="click_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">施肥需要耗费金币数</label>
                        <div class="col-md-12">
                            <input type="text" name="fertilization_cost_coin" id="fertilization-cost-coin"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">施肥效果持续天数</label>
                        <div class="col-md-12">
                            <input type="text" name="fertilization_keep_days" id="fertilization-keep-days"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <div class="form-group">
                        <label class="col-md-12">施肥后浇水时增加的能量翻倍倍数</label>
                        <div class="col-md-12">
                            <input type="text" name="fertilization_energy" id="fertilization-energy"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">施肥图片</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="fertilization-img-url" class="dropify" name="fertilization_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">施肥操作图片</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="fertilization-click-img-url" class="dropify" name="fertilization_click_img_url"
                                   data-height="200px"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <label class="col-md-12">游戏简介(ios)</label>
                <div class="col-md-12 col-sm-12">
                    <textarea name="contents" class="form-control" id="summernote" cols="30"
                              rows="5"></textarea>
                </div>
            </div>
            <div class="row">
                <label class="col-md-12">游戏简介(android)</label>
                <div class="col-md-12 col-sm-12">
                    <textarea name="contents" class="form-control" id="summernote1" cols="30"
                              rows="5"></textarea>
                </div>
            </div>

        </form>
        <div class="form-group">
            <button class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="publish">添加
            </button>
            {!! csrf_field() !!}
            <a class="fcbtn btn btn-info btn-outline btn-1e waves-effect" href="{{ url('/game/list') }}">返回
            </a>
        </div>
    </div>

@endsection
@section('js')
    @parent
    <script src="{{ asset('js/myself.js') }}"></script>

    <script>
        $("#publish").click(function (e) {
            e.preventDefault();
            var name = $("#name").val();
            var sum_energy = $("#sum-energy").val();
            var own_frequency = $("#own-frequency").val();
            var own_add_energy = $("#own-add-energy").val();
            var old_friend_frequency = $("#old-friend-frequency").val();
            var old_friend_add_energy = $("#old-friend-add-energy").val();
            var new_friend_frequency = $("#new-friend-frequency").val();
            var new_friend_add_energy = $("#new-friend-add-energy").val();
            var fertilization_cost_coin = $("#fertilization-cost-coin").val();
            var fertilization_keep_days = $("#fertilization-keep-days").val();
            var fertilization_energy = $("#fertilization-energy").val();
            var ios_rule_description = $("#summernote").val();
            var android_rule_description = $("#summernote1").val();
            var operating_name = $("#operating-name").val();
            var bg_img = document.getElementById("bg-img-url").files;
            var operating_img = document.getElementById("operating-img-url").files;
            var cover_img = document.getElementById("cover-img-url").files;
            var floor_img = document.getElementById("floor-img-url").files;
            var front_img = document.getElementById("front-img-url").files;
            var click_img = document.getElementById("click-img-url").files;
            var fertilization_img = document.getElementById("fertilization-img-url").files;
            var fertilization_click_img = document.getElementById("fertilization-click-img-url").files;


            if (!cover_img.length) {
                swal("请上传背景图片!");
                return false;
            }
            if ((cover_img[0].type).indexOf("image/") === -1) {
                swal("请上传背景图片!");
                return false;
            }
            if (!bg_img.length) {
                swal("请上传背景图片!");
                return false;
            }
            if ((bg_img[0].type).indexOf("image/") === -1) {
                swal("请上传背景图片!");
                return false;
            }

            if (!operating_img.length) {
                swal("请上传操作功能图片!");
                return false;
            }
            if ((operating_img[0].type).indexOf("image/") === -1) {
                swal("请上传操作功能图片!");
                return false;
            }

            if (!floor_img.length) {
                swal("请上传第一层图片!");
                return false;
            }
            if ((floor_img[0].type).indexOf("image/") === -1) {
                swal("请上传第一层图片!");
                return false;
            }

            if (!front_img.length) {
                swal("请上传第二层图片!");
                return false;
            }
            if ((front_img[0].type).indexOf("image/") === -1) {
                swal("请上传第二层图片!");
                return false;
            }

            if (!click_img.length) {
                swal("请上传浇水时图片!");
                return false;
            }
            if ((click_img[0].type).indexOf("image/") === -1) {
                swal("请上传浇水时图片!");
                return false;
            }


            if (!fertilization_img.length) {
                swal("请上传施肥图片!");
                return false;
            }
            if ((fertilization_img[0].type).indexOf("image/") === -1) {
                swal("请上传施肥图片!");
                return false;
            }


            if (!fertilization_click_img.length) {
                swal("请上传施肥操作图片!");
                return false;
            }
            if ((fertilization_click_img[0].type).indexOf("image/") === -1) {
                swal("请上传施肥操作图片!");
                return false;
            }


            swal({
                    title: "",
                    text: "确定添加么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    var cover_img_url = picajax(cover_img[0]);
                    var bg_img_url = picajax(bg_img[0]);
                    var operating_img_url = picajax(operating_img[0]);
                    var floor_img_url = picajax(floor_img[0]);
                    var front_img_url = picajax(front_img[0]);
                    var click_img_url = picajax(click_img[0]);
                    var fertilization_img_url = picajax(fertilization_img[0]);
                    var fertilization_click_img_url = picajax(fertilization_click_img[0]);

                    $.ajax({

                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/game/publish",
                        type: "POST",
                        data: {
                            name: name,
                            sum_energy: sum_energy,
                            own_frequency: own_frequency,
                            own_add_energy: own_add_energy,
                            old_friend_frequency: old_friend_frequency,
                            old_friend_add_energy: old_friend_add_energy,
                            new_friend_frequency: new_friend_frequency,
                            ios_rule_description: ios_rule_description,
                            android_rule_description: android_rule_description,
                            operating_name: operating_name,
                            new_friend_add_energy: new_friend_add_energy,
                            fertilization_cost_coin: fertilization_cost_coin,
                            fertilization_keep_days: fertilization_keep_days,
                            fertilization_energy: fertilization_energy,
                            cover_img_url: cover_img_url,
                            bg_img_url: bg_img_url,
                            floor_img_url: floor_img_url,
                            front_img_url: front_img_url,
                            click_img_url: click_img_url,
                            operating_img_url: operating_img_url,
                            fertilization_img_url: fertilization_img_url,
                            fertilization_click_img_url: fertilization_click_img_url
                        },
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    window.location.href = '/game/list';
                                });
                            }
                        }
                    })
                }
            );
        });
    </script>
@endsection